/**
 * 基于美国洛杉矶时间的时间类
 */
export default class USDate {
    public static readonly DAY_MILLISECONDS = 86400000;
    public static readonly HOUR_MILLISECONDS = 3600000;
    public static readonly MINUTE_MILLISECONDS = 60000;
    public static readonly SECOND_MILLISECONDS = 1000;
    public static readonly TIME_ZONE = 'America/Los_Angeles';
    // private static readonly formatter = new Intl.DateTimeFormat('en-US', {
    //     timeZone: USDate.TIME_ZONE,
    //     year: 'numeric',
    //     month: '2-digit',
    //     day: '2-digit',
    //     hour: '2-digit',
    //     minute: '2-digit',
    //     second: '2-digit',
    //     hour12: false
    // });
    // 时差
    private static _timeDifference: number;
    public static get timeDifference(): number {
        return USDate._timeDifference;
    }

    static {
        // 获得本地时间和美国洛杉矶时间的时差
        const localDate = new Date();
        const timeZoneOffset = new Date().getTimezoneOffset();
	    const utcOffset = -timeZoneOffset / 60;
        const losAngelesOffset = -8;
        this._timeDifference = (utcOffset - losAngelesOffset) * this.HOUR_MILLISECONDS;
        // const losAngelesDate = new Date(
        //     USDate.formatter.format(localDate)
        // );
        // // 计算时间差（以毫秒为单位）
        // this._timeDifference = localDate.getTime() - losAngelesDate.getTime();
        console.log('timeDifference', this._timeDifference);
        // console.log(localDate);
        // console.log(losAngelesDate);
    }

    public static now() {
        // return Date.now() - USDate.timeDifference;
        return Date.now();
    }
    /**将格式化的字符串转为时间戳 */
    public static str2Timestamp(str: string): number {
        const d = new Date(str);
        return d.getTime() + USDate.timeDifference;
    }
    /**当前是第几天 */
    public static dayIdx(time = USDate.now()) {
        // 洛杉矶是UTC-8，所以减去8小时
        // return Math.floor(time / USDate.DAY_MILLISECONDS);
        const laUtc = -8;
        return Math.floor((time + this.HOUR_MILLISECONDS * laUtc) / USDate.DAY_MILLISECONDS);
    }
    public static todayNow() {
        const laUtc = -8;
        return (Date.now() + laUtc * USDate.HOUR_MILLISECONDS) % USDate.DAY_MILLISECONDS;
    }
    public static getTomorrowCountdown() {
        return USDate.DAY_MILLISECONDS - USDate.todayNow();
    }

    public static countdown2str(ms: number) {
        const seconds = Math.floor(ms / 1000);
        const minutes = Math.floor(seconds / 60);
        const hours = Math.floor(minutes / 60);
        const days = Math.floor(hours / 24);
        if (days > 0) {
            return `${days}d${hours % 24}h`;
        } else if (hours > 0) {
            return `${hours}h${minutes % 60}m`;
        } else if (minutes > 0) {
            return `${minutes}m${seconds % 60}s`;
        } else {
            return `${seconds}s`;
        }
    }
    public static countdown2str2(ms: number) {
        // 修改下，如果格式化的字符串后面一位是0，则只显示前面的
        let seconds = Math.floor(ms / 1000);
        let minutes = Math.floor(seconds / 60);
        let hours = Math.floor(minutes / 60);
        let days = Math.floor(hours / 24);
        if (days > 0) {
            hours = hours % 24;
            return `${days}d` + (hours > 0 ? `${hours}h` : '');
        } else if (hours > 0) {
            minutes = minutes % 60;
            return `${hours}h` + (minutes > 0 ? `${minutes}m` : '');
        } else if (minutes > 0) {
            seconds = seconds % 60;
            return `${minutes}m` + (seconds > 0 ? `${seconds}s` : '');
        } else {
            return `${seconds}s`;
        }
    }
    public static msToTime(duration: number): string {
        const seconds = Math.floor((duration / 1000) % 60);
        const minutes = Math.floor((duration / (1000 * 60)) % 60);
        const hours = Math.floor((duration / (1000 * 60 * 60)) % 24);
        const minutesStr = (minutes < 10) ? "0" + minutes : minutes;
        const secondsStr = (seconds < 10) ? "0" + seconds : seconds;
        if (hours > 0) {
            return hours + ":" + minutesStr + ":" + secondsStr;
        } else {
            return minutesStr + ":" + secondsStr;
        }
    }
}